/*
 * @Author: shiguang
 * @Date: 2023-06-01 17:08:05
 * @LastEditors: shiguang
 * @LastEditTime: 2023-06-17 14:37:59
 * @Description: useFormModal
 */
import { Form } from 'antd';
import { Dispatch, SetStateAction, useState } from 'react';

// const xx = new Map<any, {title: string; }>([
//     [12, { title:  }]
// ]);
interface UseFormModalOptions{
    close?: () => void;
    open?: () => void;
    beforeClose?: (setIsOpen: Dispatch<SetStateAction<boolean>>) => void;
    beforeOpen?: UseFormModalOptions['beforeClose'];
    afterClose?: UseFormModalOptions['beforeClose'];
    afterOpen?: UseFormModalOptions['beforeClose'];
    multiple?: ({ key: string;} & Omit<UseFormModalOptions, 'multiple'>)[];
}

export const useFormModal = <Values extends any = any>(options: UseFormModalOptions = {}) => {
    const { close, open, beforeClose, beforeOpen, afterClose, afterOpen, multiple = [] } = options;
    const [isOpen, setIsOpen] = useState(false);
    const [form] = Form.useForm<Values>();
    const [instatnceList, setInstatnceList] = useState(
        multiple.map((item, index) => {
            const _setInstatnceList = (fn: ((b: boolean) => boolean) | boolean) => {
                setInstatnceList((_instatnceList) => {
                    return _instatnceList.map((it, idx) => {
                        if(idx === index){
                            return { ...it, isOpen: typeof fn === 'function' ? fn(it.isOpen) : fn };
                        }
                        return it;
                    });
                });
            };
            const curItem = {
                isOpen: false,
                form,
                modal: {
                    close: item.close ?? (() => {
                        item.beforeClose?.(_setInstatnceList);
                        _setInstatnceList(false);
                        // form.resetFields();
                        item.afterClose?.(_setInstatnceList);
                    }),
                    open: item.close ?? (() => {
                        item.beforeOpen?.(_setInstatnceList);
                        _setInstatnceList(true);
                        item.afterOpen?.(_setInstatnceList);
                    }),
                }
            };
            return curItem;
        })
    );

    // const createInstance = (modalKey: string) => {
    //     return {
    //         open: Boolean(instatnceList[modalKey]),
    //         modal: formModal,
    //         form
    //     };
    // };

    const formModal = {
        close: close ?? (() => {
            form.resetFields();
            beforeClose?.(setIsOpen);
            setIsOpen(false);
            afterClose?.(setIsOpen);
        }),
        open: open ?? (() => {
            beforeOpen?.(setIsOpen);
            setIsOpen(true);
            afterOpen?.(setIsOpen);
        }),
    };
    return {
        isOpen,
        modal: formModal,
        form,
        multiple: instatnceList
    };
};

export type FormModalInstance<Values extends any = any> = ReturnType<typeof useFormModal<Values>>;

export type FormModalContentProps<Values extends any = any, T extends Record<string, any> = {}> = T & {
    formModal: Omit<FormModalInstance<Values>, 'multiple'> & Partial<Pick<FormModalInstance<Values>, 'multiple'>>;
}